<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Company\Furniture;
use App\Models\Encyclopedia\Article;
use App\Models\Company\Furniture\Category;

class FurnitureController extends Controller
{

    public function list()
    {
        $url = request()->url();
        if (request()->get('clear', '')) {
            \Cache::clear();
        }
        $categorys = Category::get();
        if (\Cache::offsetExists($url)) {
            $categorys = \Cache::get($url);
        } else if ($categorys->isNotEmpty()) {
            $categorys->each(function (Category $backstageCategory) {
                $backstageCategory->load('isStateFurniture.region');
            });
            $categorys = \Cache::remember($url, 1440, function () use ($categorys) {
                return $categorys;
            });
        }
        $article = Article::inRandomOrder()->with('media')->take(12)->get();
        $article = $article->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return view('region.furniture', compact('categorys', 'article'));
    }

    public function jiaju($uri = null)
    {
        $category = Category::where('uri', $uri)->first();
        $city = config('region.region.model', '');
        $model = Furniture::with('region')->where('is_state', true)
            ->where('category_id', $category->id)
            ->where(function ($query) use ($city) {
                if ($city && !is_domain_top()) {
                    $query->where('region_id', $city->id);
                }else{
                    $query->where('is_top', true);
                }
            });
        $furnitures = $model->orderBy('updated_at', 'desc')->paginate(15);
        if (request()->get('clear', '')) {
            \Cache::clear();
        }
        if (\Cache::offsetExists($uri)) {
            $cache = \Cache::get($uri);
        } else {
            $furniture_rand = $model->inRandomOrder()->take(6)->get();
            $article = Article::inRandomOrder()->with('media')->take(12)->get();
            $article = $article->map(function (Article $item) {
                $item->media->path = \Storage::url($item->media->path);
                return $item;
            });
            $cache = \Cache::remember($uri, 1440, function () use ($furniture_rand, $article) {
                return ['furniture_rand' => $furniture_rand, 'article' => $article];
            });
        }
        $furniture_rand = $cache['furniture_rand'];
        $article = $cache['article'];
        return view('region.jiaju', compact('furnitures', 'furniture_rand', 'article', 'category'));
    }

    public function jiajuDetail($id)
    {
        $furniture = Furniture::where('is_state', true)->with('region')->find($id);
        $title = null;
        if ($furniture && strpos($furniture->name, '装修') !== false) {
            $title = '装修';
            $article = Article::where('title', 'like', '%装修%')->inRandomOrder()->take(6)->get();
        } elseif ($furniture && strpos($furniture->name, '装饰') !== false) {
            $title = '装饰';
            $article = Article::where('title', 'like', '%装饰%')->inRandomOrder()->take(6)->get();
        } elseif ($furniture && strpos($furniture->name, '装潢') !== false) {
            $title = '装潢';
            $article = Article::where('title', 'like', '%装潢%')->inRandomOrder()->take(6)->get();
        } elseif ($furniture && strpos($furniture->name, '装璜') !== false) {
            $title = '装璜';
            $article = Article::where('title', 'like', '%装璜%')->inRandomOrder()->take(6)->get();
        } else {
            $article = Article::inRandomOrder()->take(6)->get();
        }

        $sheji = Article::where('title', 'like', '%设计%')->inRandomOrder()->take(6)->get();

        $sheji = $sheji->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });

        $randFurniture = Furniture::where('is_state', true)
            ->where('name', 'like', '%' . $title . '%')
            ->where('region_id', $material->region_id ?? 0)->take(12)->get();

        $article = $article->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return view('region.jiaju_detail', compact('furniture', 'article', 'randFurniture', 'sheji'));
    }
}